package org.apak.frontend;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apak.db.RepairServiceDAO;
import org.apak.log.FileLogger;
import org.apak.model.Customer;
import org.apak.model.LoggedSubject;
import org.apak.model.Person;
import org.apak.model.ServiceRequest;

/**
 * Servlet implementation class ServiceRequestServlet
 */
@WebServlet("/ServiceRequestServlet")
public class ServiceRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private RepairServiceDAO dao = new RepairServiceDAO();
	private HttpSession session;
	LoggedSubject loggedSubject;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		loggedSubject = (LoggedSubject) session.getAttribute("loggedSubject");
		if(loggedSubject == null || loggedSubject.isCustomer()) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		} else {
			String service_request_id = request.getParameter("id");
			ServiceRequest currentServiceRequest = (ServiceRequest) session.getAttribute("currentServiceRequest");
			String customer = request.getParameter("customer");
			boolean sendError = false;
			if(service_request_id != null) {
				if(dao.getServiceOrderByServiceRequest(service_request_id) == null)
					request.setAttribute("cancel", true);
				ServiceRequest serviceRequest = dao.getServiceRequest(service_request_id);
				if(serviceRequest == null) {
					sendError = true;
				}
				if(serviceRequest != null && serviceRequest.getService_request_status_type_fk() == 2) {
					request.setAttribute("cancel", false);
					//System.out.println("TRUE");
				}
				session.setAttribute("currentServiceRequest", serviceRequest);
				request.setAttribute("serviceRequest", serviceRequest);
			} else if(currentServiceRequest != null && customer != null) {
				if(dao.getCustomer(customer) == null) {
					RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
					dispatcher.forward(request, response);
					return;
				}
				currentServiceRequest.changeCustomer(dao.getCustomer(customer));
				request.setAttribute("serviceRequest", currentServiceRequest);
			}
			else if(customer != null) {
				ServiceRequest newServiceRequest = new ServiceRequest();
				newServiceRequest.changeCustomer(dao.getCustomer(customer));
				session.setAttribute("currentServiceRequest", newServiceRequest);
				request.setAttribute("serviceRequest", newServiceRequest);
			}
			FileLogger.logMessage("Forwarded to serviceRequest.jsp by ServiceRequestServlet.doGet().");
			RequestDispatcher dispatcher;
			if(sendError)
				dispatcher = request.getRequestDispatcher("error.jsp");
			else
				dispatcher = request.getRequestDispatcher("serviceRequest.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher;
		String actionButton = request.getParameter("actionButton");
		ServiceRequest currentServiceRequest = (ServiceRequest) session.getAttribute("currentServiceRequest");
		if(actionButton != null)
			if(currentServiceRequest != null) {
				if(actionButton.equals("Save request"))
					if(currentServiceRequest.getService_request() != 0) {
						if(dao.getServiceOrderByServiceRequest(Integer.toString(currentServiceRequest.getService_request())) == null
								&& currentServiceRequest.getService_request_status_type_fk() != 2)
							request.setAttribute("cancel", true);
						currentServiceRequest.changeDescription(request.getParameter("clientsDescription"), request.getParameter("employeeDescription"));
						dao.updateServiceRequest(currentServiceRequest);
						request.setAttribute("serviceRequest", currentServiceRequest);
						request.setAttribute("success", "Saved sucessfully");
						session.setAttribute("currentServiceRequest", currentServiceRequest);
						FileLogger.logMessage("Forwarded to serviceRequest.jsp by ServiceRequestServlet.doPost().");
						dispatcher = request.getRequestDispatcher("serviceRequest.jsp");
						dispatcher.forward(request, response);
					} else {
						if(dao.getServiceOrderByServiceRequest(Integer.toString(currentServiceRequest.getService_request())) == null
								&& currentServiceRequest.getService_request_status_type_fk() != 2)
							request.setAttribute("cancel", true);
						currentServiceRequest.changeDescription(request.getParameter("clientsDescription"), request.getParameter("employeeDescription"));
						currentServiceRequest = dao.getServiceRequest(Integer.toString(dao.insertServiceRequest(currentServiceRequest, loggedSubject.getSubject_id())));
						request.setAttribute("serviceRequest", currentServiceRequest);
						session.setAttribute("currentServiceRequest", currentServiceRequest);
						request.setAttribute("success", "Saved sucessfully");
						FileLogger.logMessage("Forwarded to serviceRequest.jsp by ServiceRequestServlet.doPost().");
						dispatcher = request.getRequestDispatcher("serviceRequest.jsp");
						dispatcher.forward(request, response);
					}
				else if(actionButton.equals("Form order")) {
					FileLogger.logMessage("Forwarded to /serviceOrder by ServiceRequestServlet.doPost().");
					response.sendRedirect("./serviceOrder?serviceRequest=" + currentServiceRequest.getService_request());
				}
				else if(actionButton.equals("Cancel request")) {
					dao.cancelServiceRequest(currentServiceRequest.getService_request());
					FileLogger.logMessage("Forwarded to serviceRequest by ServiceRequestServlet.doPost().");
					response.sendRedirect("./serviceRequest?id=" + currentServiceRequest.getService_request());
				}
			}
			if(actionButton.equals("Search customer")) {
				FileLogger.logMessage("Forwarded to /customerMenu by ServiceRequestServlet.doPost().");
				response.sendRedirect("./customerMenu");
			} 
			
	}

}
